/**
 * @file dialog_shotclock_settings.c
 * @brief Shot Clock settings dialog implementation
 */

#include <windows.h>
#include <wchar.h>
#include <stdio.h>
#include <strsafe.h>
#include "dialog/dialog_shotclock_settings.h"
#include "dialog/dialog_common.h"
#include "dialog/dialog_language.h"
#include "config.h"
#include "shotclock.h"
#include "language.h"
#include "../resource/resource.h"

static INT_PTR CALLBACK ShotClockSettingsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
    (void)lParam;
    switch (msg) {
        case WM_INITDIALOG: {
            DialogContext* ctx = Dialog_CreateContext();
            if (ctx) {
                Dialog_SetContext(hwndDlg, ctx);
            }
            ApplyDialogLanguage(hwndDlg, CLOCK_IDD_SHOTCLOCK_SETTINGS_DIALOG);
            Dialog_CenterOnPrimaryScreen(hwndDlg);
            Dialog_ApplyTopmost(hwndDlg);

            ShotClock_InitFromConfig();
            int primary = ShotClock_GetPrimaryResetSeconds();
            int secondary = ShotClock_GetSecondaryResetSeconds();

            wchar_t wPrimary[32];
            wchar_t wSecondary[32];
            StringCchPrintfW(wPrimary, 32, L"%d", primary);
            StringCchPrintfW(wSecondary, 32, L"%d", secondary);
            SetDlgItemTextW(hwndDlg, IDC_SHOTCLOCK_PRIMARY_EDIT, wPrimary);
            SetDlgItemTextW(hwndDlg, IDC_SHOTCLOCK_SECONDARY_EDIT, wSecondary);

            char config_path[MAX_PATH] = {0};
            GetConfigPath(config_path, sizeof(config_path));
            BOOL sc_topmost = ReadIniBool(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_TOPMOST", TRUE, config_path);
            BOOL sc_clickthrough = ReadIniBool(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_CLICKTHROUGH", FALSE, config_path);
            BOOL sc_inherit = ReadIniBool(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_INHERIT_EDIT_MODE", FALSE, config_path);

            CheckDlgButton(hwndDlg, IDC_SHOTCLOCK_TOPMOST_CHECK, sc_topmost ? BST_CHECKED : BST_UNCHECKED);
            CheckDlgButton(hwndDlg, IDC_SHOTCLOCK_CLICKTHROUGH_CHECK, sc_clickthrough ? BST_CHECKED : BST_UNCHECKED);
            CheckDlgButton(hwndDlg, IDC_SHOTCLOCK_INHERIT_EDIT_CHECK, sc_inherit ? BST_CHECKED : BST_UNCHECKED);

            return TRUE;
        }

        case WM_CTLCOLORDLG:
        case WM_CTLCOLORSTATIC:
        case WM_CTLCOLOREDIT:
        case WM_CTLCOLORBTN: {
            DialogContext* ctx = Dialog_GetContext(hwndDlg);
            INT_PTR result;
            if (Dialog_HandleColorMessages(msg, wParam, ctx, &result)) {
                return result;
            }
            break;
        }

        case WM_COMMAND: {
            switch (LOWORD(wParam)) {
                case IDOK: {
                    wchar_t wPrimary[64] = {0};
                    wchar_t wSecondary[64] = {0};
                    GetDlgItemTextW(hwndDlg, IDC_SHOTCLOCK_PRIMARY_EDIT, wPrimary, 64);
                    GetDlgItemTextW(hwndDlg, IDC_SHOTCLOCK_SECONDARY_EDIT, wSecondary, 64);

                    if (Dialog_IsEmptyOrWhitespace(wPrimary) || Dialog_IsEmptyOrWhitespace(wSecondary)) {
                        Dialog_ShowErrorAndRefocus(hwndDlg, IDC_SHOTCLOCK_PRIMARY_EDIT);
                        return TRUE;
                    }

                    int primary = _wtoi(wPrimary);
                    int secondary = _wtoi(wSecondary);
                    if (primary <= 0 || secondary <= 0) {
                        Dialog_ShowErrorAndRefocus(hwndDlg, IDC_SHOTCLOCK_PRIMARY_EDIT);
                        return TRUE;
                    }

                    UpdateConfigIntAtomic(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_PRIMARY_SECONDS", primary);
                    UpdateConfigIntAtomic(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_SECONDARY_SECONDS", secondary);

                    BOOL sc_topmost = IsDlgButtonChecked(hwndDlg, IDC_SHOTCLOCK_TOPMOST_CHECK) == BST_CHECKED;
                    BOOL sc_clickthrough = IsDlgButtonChecked(hwndDlg, IDC_SHOTCLOCK_CLICKTHROUGH_CHECK) == BST_CHECKED;
                    BOOL sc_inherit = IsDlgButtonChecked(hwndDlg, IDC_SHOTCLOCK_INHERIT_EDIT_CHECK) == BST_CHECKED;

                    UpdateConfigBoolAtomic(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_TOPMOST", sc_topmost);
                    UpdateConfigBoolAtomic(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_CLICKTHROUGH", sc_clickthrough);
                    UpdateConfigBoolAtomic(INI_SECTION_SHOTCLOCK, "SHOTCLOCK_INHERIT_EDIT_MODE", sc_inherit);
                    FlushConfigToDisk();

                    ShotClock_InitFromConfig();

                    HWND hShotClock = ShotClock_GetHwnd();
                    if (hShotClock) {
                        SetClickThrough(hShotClock, sc_clickthrough);
                        SetWindowPos(hShotClock, sc_topmost ? HWND_TOPMOST : HWND_NOTOPMOST,
                                     0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                    }

                    EndDialog(hwndDlg, IDOK);
                    return TRUE;
                }
                case IDCANCEL: {
                    EndDialog(hwndDlg, IDCANCEL);
                    return TRUE;
                }
            }
            break;
        }
        case WM_DESTROY: {
            DialogContext* ctx = Dialog_GetContext(hwndDlg);
            if (ctx) {
                Dialog_FreeContext(ctx);
                Dialog_SetContext(hwndDlg, NULL);
            }
            break;
        }
    }
    return FALSE;
}

void ShowShotClockSettingsDialog(HWND hwndParent) {
    DialogBoxW(GetModuleHandle(NULL),
              MAKEINTRESOURCE(CLOCK_IDD_SHOTCLOCK_SETTINGS_DIALOG),
              hwndParent,
              ShotClockSettingsDlgProc);
}